(function(){
    angular.module('app').controller("dspAdvCtrl",function($scope,proxy){
        $scope.permission = PERMISSION;
        $scope.stateList = [
            {name:"全部状态"},
            {id:"1",name:"待审核"},
            {id:"2",name:"审核通过"},
            {id:"3",name:"审核拒绝"},
        ]
        // 显示资质弹窗
        $scope.showAudit = function (item) {
            $scope.advertiserID = item.id;
            $scope.auditState = item.auditState;
            console.log(item);
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/advertiser/qualification/list',
                load: true,
                data: {
                    "sort": ["cTime,1"],
                    filter: {
                        orgID: item.id
                    }
                },
                success: function(res){
                    // 获得列表
                    $scope.quaList = res.data;
                    $("#quaModal").modal();
                }
            })
        }
        $scope.filter = {}
        $scope.selectState = function (item) {
            $scope.filter.auditState = item.id;
            getList();
        }
        $scope.search = function() {
            getList();
        }
        // 审核
        $scope.audit = function (state) {
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/admin/advertiser/audit',
                load: true,
                data: {
                    data: {
                        advertiserID: [$scope.advertiserID],
                        auditState: state+""
                    }
                },
                success: function(){
                    layer.msg("操作成功!")
                    $("#quaModal").modal("hide");
                    getList();
                }
            })
        }
        // 显示相片弹框
        $scope.showPhoto = function(url){
            if(url) {
                layer.photos({
                    photos:{
                        "title": "", //相册标题
                        "id": 1, //相册id
                        "start": 0, //初始显示的图片序号，默认0
                        "data": [   //相册包含的图片，数组格式
                            {
                                "alt": "",
                                "pid": 2, //图片id
                                "src": url, //原图地址
                                "thumb": "" //缩略图地址
                            }
                        ]
                    },
                    anim: 5
                })
            }
        }
        getList();

        function getList(current, pagesize) {
            var data = {
                'page': (current || 1)+"",
                'size': (pagesize || 20)+"",
                "sort": ["cTime,1"],
                'filter': $scope.filter
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/advertiser/list',
                load: true,
                data: data,
                success: function(res){
                    // 获得列表
                    $scope.list = res.data.data;
                    var ids = [];
                    $scope.list.forEach(function (v) {
                        ids.push(v.id)
                    })
                    getAdxAudit(ids);

                    $("#pageToolbar").html("");
                    $('#pageToolbar').Paging({
                        pagesize: pagesize || '20',
                        current: current || '1',
                        count:res.data.total,
                        toolbar:true,
                        'hash': false,
                        callback:getList
                    });
                }
            })
        }

        function getAdxAudit(ids){
            if(ids.length == 0) return;
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/advertiser/adx/auditList',
                load: true,
                data: {
                    data: ids
                },
                success: function(res){
                    // 获得列表
                    ids.forEach(function(v,i){
                        $scope.list[i].adxList = res.data[v];
                    })
                }
            })
        }

    })
})();